//
//  StringReverserActivity.swift
//  1.13.Presenting Custom Sharing Options with UIActivityViewController
//
//  Created by joker on 15/7/22.
//  Copyright (c) 2015年 joker. All rights reserved.
//

import UIKit

class StringReverserActivity: UIActivity {

    var activityItems = [NSString]()
    
    override func activityType() -> String? {
        
        return NSBundle.mainBundle().bundleIdentifier! + ".StringReverserActivity"
    }
    
    override func activityTitle() -> String? {
        return "Reverse String"
    }
    
    override func activityImage() -> UIImage? {
        return UIImage(named: "Reverse")!
    }
    
    override func canPerformWithActivityItems(activityItems: [AnyObject]) -> Bool {
        for object: AnyObject in activityItems {
            if object is String{
                return true
            }
        }
        return false
    }
    
    override func prepareWithActivityItems(paramActivityItems: [AnyObject]) {
        for object: AnyObject in paramActivityItems {
            if object is String {
                activityItems.append(object as! String)
            }
        }
    }
    
    func reverseOfString(string: NSString) -> NSString {
        var result = ""
        var characters = [Character]()
        
        for character in string as String {
            characters.append(character)
        }
        
        for character in characters.reverse() {
            result += "\(character)"
        }
        return result
    }
    
    override func performActivity() {
        var reversedStrings = ""
        for string in activityItems {
            reversedStrings += (reverseOfString(string) as String) + "\n"
        }
        println(reversedStrings)
        
    }
}
